#
#Extracts contacts from vCard(vcf) as CSV files which can then be exported to any database
#Usage: cat *.vcf  | ContactsExtractor.pl 
# 	BUGS: Doesnt support name encoding eg: N;ENCODING=QUOTED-PRINTABLE:=7Ehome=7E;;;; for ~home~
#Version 0.1 Anand 20101111
#
use strict;

my $contactFile = "contact.csv";
my $numberFile = "number.csv";

open(CONTACTFILE, ">$contactFile");
open(NUMBERFILE, ">$numberFile");

print "Following rows are unprocessed:\n";

my $id = 0;
while(<>)
{
	if(/^BEGIN|^VERSION|^END|^X-CLASS/) #skip useless vCard info
	{
        next;
    }
	
	#name
    if(/^N:(.*)/)
    {
	  $id++;
	  my @fields = split ';', $1 ;
	  print CONTACTFILE "$id,$fields[1]$fields[0]\n";
    }
	else
	{ 
		if(/^TEL.*:(.*)/)
		{
		   print NUMBERFILE "$id,$1\n";
		}
		else
		{ 
		   #print unread rows
		   print ;	
		}	
	}	   
}

close CONTACTFILE;
close NUMBERFILE;